package com.yanqu.road.dao.impl.activity.peakseacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftCrossChallengeData;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftTeamInfo;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 */
public class PeakSeacraftCrossChallengeDataDaoImpl extends TempDataObjectDao<PeakSeacraftCrossChallengeData> {

    public static String getPeakSeacraftCrossChallengeDataSql = "select * from `t_c_activity_peak_seacraft_challenge` where `activityId`=? and unionGroup=?";
    public Map<Long, PeakSeacraftCrossChallengeData> getPeakSeacraftCrossChallengeDataMap(int activityId, int unionGroup) {
        Map<Long, PeakSeacraftCrossChallengeData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getPeakSeacraftCrossChallengeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                PeakSeacraftCrossChallengeData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updatePeakSeacraftCrossChallengeDataSql = "update `t_c_activity_peak_seacraft_challenge` set targetId=?,attackType=?,teamType=?,patronsInfoList=?,winStreakNum=?,receiveWinStreakTimes=?,buyBuffId=?,attackedList=?,randomMap=?,buffValueList=?,lockDurable=?,deductDurable=?,totalScore=?,distance=?,targetDistance=?,killPatronsNum=?,targetUnionRank=?,winStreakReward=?,targetUnionGroupRank=? where activityId=? and unionGroup=? and userId=?";
    @Override
    protected boolean updateData(PeakSeacraftCrossChallengeData peakSeacraftCrossChallengeData) {
        peakSeacraftCrossChallengeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossChallengeData.getTargetId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getAttackType()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getTeamType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossChallengeData.getPatronsInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getWinStreakNum()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getReceiveWinStreakTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getBuyBuffId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossChallengeData.getAttackedList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossChallengeData.getRandomMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossChallengeData.getBuffValueList())));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossChallengeData.getLockDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getDeductDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossChallengeData.getDistance()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossChallengeData.getTargetDistance()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getKillPatronsNum()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getTargetUnionRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(peakSeacraftCrossChallengeData.getWinStreakReward())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getTargetUnionGroupRank()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossChallengeData.getUserId()));
        if (getDb().executeNoneQuery(updatePeakSeacraftCrossChallengeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPeakSeacraftCrossChallengeDataSql = "insert into `t_c_activity_peak_seacraft_challenge`(activityId,unionGroup,userId,targetId,attackType,teamType,patronsInfoList,winStreakNum,receiveWinStreakTimes,buyBuffId,attackedList,randomMap,buffValueList,lockDurable,deductDurable,totalScore,distance,targetDistance,killPatronsNum,targetUnionRank,winStreakReward,targetUnionGroupRank) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(PeakSeacraftCrossChallengeData peakSeacraftCrossChallengeData) {
        peakSeacraftCrossChallengeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossChallengeData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossChallengeData.getTargetId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getAttackType()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getTeamType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossChallengeData.getPatronsInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getWinStreakNum()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getReceiveWinStreakTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getBuyBuffId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossChallengeData.getAttackedList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossChallengeData.getRandomMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossChallengeData.getBuffValueList())));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossChallengeData.getLockDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getDeductDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossChallengeData.getDistance()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossChallengeData.getTargetDistance()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getKillPatronsNum()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getTargetUnionRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(peakSeacraftCrossChallengeData.getWinStreakReward())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossChallengeData.getTargetUnionGroupRank()));
        if (getDb().executeNoneQuery(insertPeakSeacraftCrossChallengeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PeakSeacraftCrossChallengeData parseData(ResultSet rs) throws SQLException {
        PeakSeacraftCrossChallengeData peakSeacraftCrossChallengeData = new PeakSeacraftCrossChallengeData();
        peakSeacraftCrossChallengeData.setActivityId(rs.getInt("activityId"));
        peakSeacraftCrossChallengeData.setUnionGroup(rs.getInt("unionGroup"));
        peakSeacraftCrossChallengeData.setUserId(rs.getLong("userId"));
        peakSeacraftCrossChallengeData.setTargetId(rs.getString("targetId"));
        peakSeacraftCrossChallengeData.setAttackType(rs.getInt("attackType"));
        peakSeacraftCrossChallengeData.setTeamType(rs.getInt("teamType"));
        peakSeacraftCrossChallengeData.setPatronsInfoList(JSON.parseObject(rs.getString("patronsInfoList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftLockPatronsInfo>>() {}));
        peakSeacraftCrossChallengeData.setWinStreakNum(rs.getInt("winStreakNum"));
        peakSeacraftCrossChallengeData.setReceiveWinStreakTimes(rs.getInt("receiveWinStreakTimes"));
        peakSeacraftCrossChallengeData.setBuyBuffId(rs.getInt("buyBuffId"));
        peakSeacraftCrossChallengeData.setAttackedList(JSON.parseObject(rs.getString("attackedList"), new TypeReference<java.util.List<java.lang.Long>>() {}));
        peakSeacraftCrossChallengeData.setRandomMap(JSON.parseObject(rs.getString("randomMap"), new TypeReference<java.util.Map<java.lang.Long, PeakSeacraftTeamInfo>>() {}));
        peakSeacraftCrossChallengeData.setBuffValueList(JSON.parseObject(rs.getString("buffValueList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        peakSeacraftCrossChallengeData.setLockDurable(rs.getLong("lockDurable"));
        peakSeacraftCrossChallengeData.setDeductDurable(rs.getInt("deductDurable"));
        peakSeacraftCrossChallengeData.setTotalScore(rs.getInt("totalScore"));
        peakSeacraftCrossChallengeData.setDistance(rs.getLong("distance"));
        peakSeacraftCrossChallengeData.setTargetDistance(rs.getLong("targetDistance"));
        peakSeacraftCrossChallengeData.setKillPatronsNum(rs.getInt("killPatronsNum"));
        peakSeacraftCrossChallengeData.setTargetUnionRank(rs.getInt("targetUnionRank"));
        peakSeacraftCrossChallengeData.setWinStreakReward(PropertyHelper.parseStringToProperty(rs.getString("winStreakReward")));
        peakSeacraftCrossChallengeData.setTargetUnionGroupRank(rs.getInt("targetUnionGroupRank"));

        return peakSeacraftCrossChallengeData;
    }

}
